package com.dimples.dd.system.config.init;

import com.dimples.dd.common.constant.DDConstant;
import com.dimples.dd.redis.core.constant.RedisConstants;
import com.dimples.dd.system.service.SystemGatewayRouteService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

/**
 * @desc: 启动程序，初始化路由配置
 * @author: flyme
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class SystemInitListener implements ApplicationListener<ApplicationReadyEvent>, Ordered {


    private final SystemGatewayRouteService sysGatewayRouteService;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {

        log.info(DDConstant.CONFIG_LOG, "服务已启动，初始化路由配置");
        String context = "AnnotationConfigServletWebServerApplicationContext";
        if (applicationReadyEvent.getApplicationContext().getDisplayName().contains(context)) {
            sysGatewayRouteService.addRoute2Redis(RedisConstants.GATEWAY_ROUTES);
        }

    }

    @Override
    public int getOrder() {
        return 1;
    }
}
